package com.imy.common.base;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import butterknife.ButterKnife;

/**
 * 用于分离业务的View Created by user on 2017/2/14.
 */

public abstract class BaseCustomViewGroup extends FrameLayout implements LifecycleObserver {
  protected final String TAG = this.getClass().getSimpleName();
  protected Context mContext;

  private boolean isInit = false;
  protected AttributeSet attrs;
  private Activity activity;

  public BaseCustomViewGroup(Context context) {
    super(context);
    initConfig(context, null);
  }

  public BaseCustomViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    initConfig(context, attrs);
  }

  public BaseCustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initConfig(context, attrs);

  }

  private void initConfig(Context context, AttributeSet attributeSet) {
    if (!isInit) {
      attrs = attributeSet;
      mContext = context;
      View inflate = LayoutInflater.from(mContext).inflate(getLayoutID(), null);
      ButterKnife.bind(this, inflate);
      addView(inflate);
      init(inflate);
      isInit = true;
      if (mContext instanceof AppCompatActivity) {
        AppCompatActivity activity = (AppCompatActivity) mContext;
        activity.getLifecycle().addObserver(this);
      }
    }
  }

  protected AttributeSet getAttrs() {
    return attrs;
  }

  protected abstract void init(View inflate);

  protected abstract int getLayoutID();

  @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
  public void onDestroy() {
    Log.i(TAG, "onDestroy");

  }

}
